package org.hcf.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.hcf.data.entity.ChatFriend;
import org.hcf.data.mapper.ChatFriendMapper;
import org.hcf.data.service.IChatFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 好友表 服务实现类
 * </p>
 *
 * @author xianiyitiao
 * @since 2024-04-18
 */
@Service
public class ChatFriendServiceImpl extends ServiceImpl<ChatFriendMapper, ChatFriend> implements IChatFriendService {

    @Override
    public List<ChatFriend> getFriends(long userId) {
        return list(new LambdaQueryWrapper<ChatFriend>().eq(ChatFriend::getFromId, userId));
    }

    @Override
    public List<ChatFriend> getFriendsByTo(long to) {
        return list(new LambdaQueryWrapper<ChatFriend>().eq(ChatFriend::getToId,to));
    }

    @Override
    public ChatFriend getFriend(long fromId, long toId) {
        return getOne(new LambdaQueryWrapper<ChatFriend>().eq(ChatFriend::getFromId, fromId).eq(ChatFriend::getToId, toId));
    }

    @Override
    public boolean updateReceive(long id, boolean isReceive) {
        return update(new LambdaUpdateWrapper<ChatFriend>().eq(ChatFriend::getFriendId, id).set(ChatFriend::isReceive, isReceive));
    }

    @Override
    public List<Long> getFriendsByLastTime(long userId, LocalDateTime lastTime) {
        return list(new LambdaQueryWrapper<ChatFriend>().select(ChatFriend::getFriendId).gt(ChatFriend::getModifyTime, lastTime).eq(ChatFriend::getFromId, userId)).stream().map(ChatFriend::getFriendId).collect(Collectors.toList());
    }
}
